<!DOCTYPE html>
<!--

  @author ajp@google.com (Andy Perelson)
-->
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.editor.plugins.AbstractTabHandler</title>
<script src="../../base.js"></script>
<script src="../../deps.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.editor.BrowserFeature');
  goog.require('goog.editor.plugins.AbstractTabHandler');
  goog.require('goog.events.BrowserEvent');
  goog.require('goog.events.KeyCodes');
  goog.require('goog.testing.StrictMock');
  goog.require('goog.testing.editor.FieldMock');
  goog.require('goog.testing.jsunit');
  goog.require('goog.userAgent');
</script>
</head>
<body>

<script>

var tabHandler;
var editableField;
var handleTabKeyCalled = false;

function setUp() {
  editableField = new goog.testing.editor.FieldMock();
  editableField.inModalMode = goog.editor.Field.prototype.inModalMode;
  editableField.setModalMode = goog.editor.Field.prototype.setModalMode;

  tabHandler = new goog.editor.plugins.AbstractTabHandler();
  tabHandler.registerFieldObject(editableField);
  tabHandler.handleTabKey = function(e) {
    handleTabKeyCalled = true;
    return true;
  };
}

function tearDown() {
  tabHandler.dispose();
}

function testHandleKey() {
  var event = new goog.testing.StrictMock(goog.events.BrowserEvent);
  event.keyCode = goog.events.KeyCodes.TAB;
  event.ctrlKey = false;
  event.metaKey = false;

  assertTrue('Event must be handled when no modifier keys are pressed.',
      tabHandler.handleKeyboardShortcut(event, '', false));
  assertTrue(handleTabKeyCalled);
  handleTabKeyCalled = false;

  editableField.setModalMode(true);
  if (goog.userAgent.GECKO) {
    assertFalse('Event must not be handled when in modal mode',
        tabHandler.handleKeyboardShortcut(event, '', false));
    assertFalse(handleTabKeyCalled);
  } else {
    assertTrue('Event must be handled when in modal mode',
        tabHandler.handleKeyboardShortcut(event, '', false));
    assertTrue(handleTabKeyCalled);
    handleTabKeyCalled = false;
  }

  event.ctrlKey = true;
  assertFalse('Plugin must never handle tab key press when ctrlKey is pressed.',
      tabHandler.handleKeyboardShortcut(event, '', false));
  assertFalse(handleTabKeyCalled);

  event.ctrlKey = false;
  event.metaKey = true;
  assertFalse('Plugin must never handle tab key press when metaKey is pressed.',
      tabHandler.handleKeyboardShortcut(event, '', false));
  assertFalse(handleTabKeyCalled);
}
</script>
</body>
</html>
